﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.EntitiesInfo;
using ConvertUtility = Lib.Utility.ConvertUtility;

namespace Web.Cms.ClubName
{
    public partial class CreateEditClubName1 : AdminUserControl
    {
        #region Khai báo
        private int clubId;
        private CMSClubNameInfo info;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                clubId = ConvertUtility.ToInt32(Request.QueryString["clubId"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["clubId"]);
                
                txtTeaser.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                if (!Page.IsPostBack)
                {                    
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (clubId > 0)
                {
                    btnUpdateClubName.Visible = true;
                    btnAddClubName.Visible = false;

                    info = CMSClubName.GetInfo(clubId);
                    if (info != null)
                    {
                        txtClubName.Text = info.ClubName;
                        ImgAvatar.ImageUrl = info.Avatar;
                        ImgAvatar.Visible = true;
                        txtCreateDate.SelectedDate = info.CreatedOn;
                        txtTeaser.Text = info.Teaser;
                        txtAvatar.Text = info.Avatar;
                        txtDateEstablished.SelectedDate = info.DateEstablished;
                        txtBody.Text = info.Body;
                    }
                }
                else
                {
                    txtCreateDate.SelectedDate = DateTime.Now;
                    txtDateEstablished.SelectedDate = DateTime.Now;
                    btnUpdateClubName.Visible = false;
                    btnAddClubName.Visible = true;
                    ImgAvatar.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CMSClubNameInfo info)
        {

            info.ClubId = clubId;
            info.ClubName = txtClubName.Text;
            info.Teaser = txtTeaser.Text;
            info.Body = txtBody.Text;
            info.Status = 1;
            info.ShowType = 0;
            info.CreatedOn = DateTime.Now;
            info.CreatedBy = CurrentUser.Username;
            info.NationalId = 0;
            info.DateEstablished = txtDateEstablished.SelectedDate;
            info.Avatar = txtAvatar.Text.Trim();
        }
        #endregion


        #region thêm
        protected void btnAddClubName_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSClubNameInfo();
                FillData(info);
                if (txtClubName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên câu lạc bộ?");
                    txtClubName.Focus();
                    return;
                }
                else
                {
                    int i = CMSClubName.Insert(info);
                    if (i > 0)
                    {
                        Response.Redirect("/cms/Default.aspx?ctrlkey=ListClubNamePublic");

                    }
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }

        }
        #endregion

        #region sửa
        protected void btnUpdateClubName_Click(object sender, EventArgs e)
        {
            try
            {
                CMSClubNameInfo info = new CMSClubNameInfo();
                FillData(info);
                if (txtClubName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên câu lạc bộ?");
                    txtClubName.Focus();
                    return;
                }
                else
                {
                    CMSClubName.Update(info);
                    Response.Redirect("/cms/Default.aspx?ctrlkey=ListClubNamePublic");
                   
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }

        }

        #endregion

        #region thoát
        protected void btnCanelClubName_Click(object sender, EventArgs e)
        {
            Response.Redirect("/cms/Default.aspx?ctrlkey=ListClubNamePublic");
        }
        #endregion

        #region ClearText
        public void ClearText()
        {
            txtClubName.Text = "";
            txtTeaser.Text = "";
            txtBody.Text = "";
            txtAvatar.Text = "";
           
           
        }
        #endregion
    }
}